package model
import (
	"gopkg.in/mgo.v2"
	"oxy_server/module/log"
	"fmt"
	// "gopkg.in/mgo.v2/bson"
)

const url  = "mongodb://localhost:27017";

type Operater struct {
	MogSession *mgo.Session
	Dbname string
	Document string
}

type Person struct {
	Name    string 	`bson:"name"`
	Password string    `bson:"password"`
	Role    string  `bson:"role"`
	Email string    `bson:"email"`
	Phone string    `bson:"phone"`
}
var db *Operater

func DB() *Operater {
	if db == nil {
		mgo.SetDebug(true)
		//mgo.SetLogger()
		mgo.SetStats(true)
		log.Debugf("Model NewDB")
		db=new(Operater)
		err:=db.connect()
		if err !=nil {
			fmt.Println("连接出错",err)
			return  nil
		}
	}
	return db
}

//连接数据库
func (operater *Operater) connect() error {
	mogsession, err := mgo.Dial(url)
	if err != nil {
		fmt.Println(err)
		return err
	}
	operater.MogSession=mogsession
	return nil
}
//插入
func (operater *Operater) Insert( p interface{}) error {
	collcetion:=operater.MogSession.DB(operater.Dbname).C(operater.Document)
	err:=collcetion.Insert(p)
	return err
}

//插入
func (operater *Operater) InsertAll( p ...interface{}) error {
	collcetion:=operater.MogSession.DB(operater.Dbname).C(operater.Document)
	err:=collcetion.Insert(p...)
	return err
}

//条件查询多个
func (operater *Operater) Query(data interface{},currentpage int ,perpage int ) (*mgo.Query, int, error) {
	collcetion:=operater.MogSession.DB(operater.Dbname).C(operater.Document)
	limit,skip:= calLimitSkip(currentpage,perpage)
	query:=collcetion.Find(data).Limit(limit).Skip(skip)
	count,err:=collcetion.Find(data).Count()
	return query,count,err
}

//排序查询
func (operater *Operater) QuerySort(data interface{},currentpage int ,perpage int,sort string ) (*mgo.Query, int, error) {
	collcetion:=operater.MogSession.DB(operater.Dbname).C(operater.Document)
	limit,skip:= calLimitSkip(currentpage,perpage)
	query:=collcetion.Find(data).Sort(sort).Limit(limit).Skip(skip)
	count,err:=collcetion.Find(data).Count()
	return query,count,err
}
//条件查询一个
func (operater *Operater) QueryOne(data interface{}) (*mgo.Query,error) {
	collcetion:=operater.MogSession.DB(operater.Dbname).C(operater.Document)
	query:=collcetion.Find(data)
	return query,nil
}
// 根据ID 更新
func (operater *Operater) Update(data interface{}, update interface{}) (error) {
	collcetion:=operater.MogSession.DB(operater.Dbname).C(operater.Document)
	err:=collcetion.Update(data, update)
	return err
}

//删除
func (operater *Operater) Remove(data interface{} ) (error) {
	collcetion:=operater.MogSession.DB(operater.Dbname).C(operater.Document)
	err:=collcetion.Remove(data)
	if err != nil{
		return err
	}
	return nil
}

//删除全部
func (operater *Operater) RemoveAll(data interface{} ) (interface{},error) {
	collcetion:=operater.MogSession.DB(operater.Dbname).C(operater.Document)
	info ,err:=collcetion.RemoveAll(data)
	if err != nil{
		return nil, err
	}
	return info,nil
}

//删除表
func (operater *Operater) DropCollection() (error) {
	collcetion:=operater.MogSession.DB(operater.Dbname).C(operater.Document)
	err:=collcetion.DropCollection()
	if err != nil{
		return err
	}
	return nil
}

func calLimitSkip(current int, per int) (int ,int) {
	limit := per
	skip := (current -1) * per
	if skip <0{
		skip =0 
	} 
	return limit ,skip 
}

	
func (operater *Operater) UpdateAll(selecter interface{},update interface{}) (*mgo.ChangeInfo, error) {
	collcetion:=operater.MogSession.DB(operater.Dbname).C(operater.Document)
	changeinfo,err:=collcetion.UpdateAll(selecter,update)
	return changeinfo,err
}


// //更新一行
// func (operater *Operater) update() (error) {
// 	collcetion:=operater.MogSession.DB(operater.Dbname).C(operater.Document)
// 	update:=Person{
// 		33,
// 		"詹姆斯",
// 		201,
// 	}
// 	err:=collcetion.Update(bson.M{"name": "周杰伦"},update)
// 	if err !=nil {
// 	    fmt.Println(err)
// 	}
// 	return err
// }
// //更新所有数据


// //单行删除
// func (operater *Operater) delete(seletor interface{}) (error) {
// 	collcetion:=operater.MogSession.DB(operater.Dbname).C(operater.Document)
// 	return collcetion.Remove(seletor)
// }

// //统计文档中数据的个数
// func (operater *Operater) count() (int,error) {
// 	collcetion:=operater.MogSession.DB(operater.Dbname).C(operater.Document)
// 	i,err:=collcetion.Count()
// 	return i,err
// }
